# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Contributor: Milan P. Stanić <mps@arvanta.net>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=crystal
pkgver=1.4.1
pkgrel=1
_bootver=1.3.2
_llvmver=14
pkgdesc="The Crystal Programming Language"
url="https://crystal-lang.org/"
arch="x86_64 aarch64"
license="Apache-2.0"
depends="
	gc-dev
	gcc
	gmp-dev
	libatomic_ops
	libevent-dev
	libevent-static
	musl-dev
	pcre-dev
	"
checkdepends="
	openssl1.1-compat-dev
	tzdata
	yaml-dev
	zlib-dev
	"
makedepends="
	libxml2-dev
	llvm$_llvmver-dev
	llvm$_llvmver-static
	zlib-static
	"
subpackages="$pkgname-doc
	$pkgname-bash-completion:bashcomp:noarch
	$pkgname-zsh-completion:zshcomp:noarch
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/crystal-lang/$pkgname/archive/$pkgver.tar.gz
	https://dev.alpinelinux.org/archive/crystal/crystal-$_bootver-x86_64-alpine-linux-musl.tar.gz
	https://dev.alpinelinux.org/archive/crystal/crystal-$_bootver-aarch64-alpine-linux-musl.tar.gz
	fix-crystal-sh.patch
	"

_coredir="/usr/lib/$pkgname/core"
_shardsdir="/usr/lib/$pkgname/shards"

export CRYSTAL_CONFIG_VERSION="$pkgver"
export CRYSTAL_CACHE_DIR="$srcdir/.cache"
export LLVM_CONFIG="/usr/lib/llvm$_llvmver/bin/llvm-config"

# Build static crystal binary and upload it to dev.a.o, so it can be used
# for building crystal next time (needed for bootstrapping).
# NOTE: After https://github.com/crystal-lang/crystal/issues/5689 is fixed,
# we can also use prebuilt binary from upstream if needed.
snapshot() {
	local binary="$builddir/.build/crystal"
	local tarname="$pkgname-$pkgver-$CTARGET"

	if [ ! -f "$binary" ] || ! file "$binary" | grep -qw 'statically linked'; then
		msg "Building statically linked crystal..."
		BUILD_STATIC=1 abuild clean deps unpack prepare build
	fi

	cd "$srcdir"

	strip "$builddir"/.build/crystal
	install -D -m 755 "$builddir"/.build/crystal "$tarname"/bin/crystal
	tar -czf "$tarname.tar.gz" "$tarname"

	msg "Uploading $tarname.tar.gz to dev.alpinelinux.org"
	scp "$tarname.tar.gz" dev.alpinelinux.org:/archive/$pkgname/

	APKBUILD="../APKBUILD" abuild undeps
}

prepare() {
	default_prepare

	# FIXME: These specs fail with invalid memory access.
	rm spec/compiler/compiler_spec.cr

	cat > Makefile.local <<-EOF
		progress = 1
		threads = ${JOBS:-2}
		verbose = 1
		FLAGS = --verbose --target $CTARGET ${BUILD_STATIC:+"--link-flags=-no-pie"}
		LLVM_CONFIG = $LLVM_CONFIG
	EOF
}

build() {
	make crystal \
		CRYSTAL_CONFIG_BUILD_COMMIT= \
		CRYSTAL_CONFIG_PATH="lib:$_shardsdir:$_coredir" \
		PATH="$srcdir/$pkgname-$_bootver-$CBUILD/bin:$PATH" \
		release=1 \
		static=${BUILD_STATIC:-}
}

check() {
	make std_spec threads=1 SPEC_FLAGS='--no-color --verbose'

	find samples -name '*.cr' | xargs -tn 1 ./bin/crystal build --no-codegen

	# Takes ~1 hour on x86_64, ~1.5 hour on aarch64.
	#make compiler_spec threads=1 SPEC_FLAGS='--no-color --verbose'
}

package() {
	install -D -m 755 .build/crystal "$pkgdir"/usr/bin/crystal
	install -D -m 644 man/crystal.1 "$pkgdir"/usr/share/man/man1/crystal.1

	mkdir -p "$pkgdir$_coredir" "$pkgdir$_shardsdir"
	cp -r src/* "$pkgdir$_coredir"/

	cd "$pkgdir$_coredir"
	rm llvm/ext/llvm_ext.o
}

bashcomp() {
	pkgdesc="Bash completions for $pkgname"
	depends=""
	install_if="$pkgname=$pkgver-r$pkgrel bash-completion"

	cd "$builddir"
	install -D -m 644 etc/completion.bash \
		"$subpkgdir"/usr/share/bash-completion/completions/$pkgname
}

zshcomp() {
	pkgdesc="ZSH completions for $pkgname"
	depends=""
	install_if="$pkgname=$pkgver-r$pkgrel zsh"

	cd "$builddir"
	install -D -m 644 etc/completion.zsh \
		"$subpkgdir"/usr/share/zsh/site-functions/_$pkgname
}

sha512sums="
b9f853c51579d661c9c99ae2402d7da7b55b73234e39fcff8790e22fa6a851e6c178d802d33315dcbdcee40b628b0711de09a25cd6de344c0a87faacca9be8c6  crystal-1.4.1.tar.gz
f7bed0d34c3c635f108caa8ddb505131fd0ed8df1d521912663f5e0720394cae9c90ab0bbdb7723c8face24b0984646bb6cd05350f7f06edb460cbe2f9ff8396  crystal-1.3.2-x86_64-alpine-linux-musl.tar.gz
080197d3c0e566cc507949001c28e06d35278663e85a6520e0eb1e6ad800fbe33ee373c135cfe91a464fa024bb7419b49aa8dfb000d90b3593adfba035ba671e  crystal-1.3.2-aarch64-alpine-linux-musl.tar.gz
2ba9e26eb1e6d20681e7f2ad78e0ebd75ae0dc614624c8de90c918046d0451f43173a23981dbebb47d1da2cf58663e8b1169d7f32799b054fc0f6a6cb112b7c5  fix-crystal-sh.patch
"
